import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {AddEventService} from './event.service';
import {BaseComponent} from '../common/base.component';
import {AddEventPage, AddEvent} from './event';
import {FileUploader} from 'ng2-file-upload';
import {ParamConfig} from '../common/param.config';

declare var $: any;

@Component({
  selector: 'app-event',
  templateUrl: './event.component.html'
})
export class EventComponent extends BaseComponent implements OnInit {
  uploader: FileUploader = new FileUploader({
    url: ParamConfig.HTTP_WEB_HOST_URL + 'rbFile/upload?type=3',
    method: 'POST',
    itemAlias: 'file',
    removeAfterUpload: true
  });
  editorTitle: String = '';
  isAddEditor: boolean;
  curPage: Number = 1;
  fileId: string;
  addUserPage: AddEventPage;
  addEvent: AddEvent[];
  curaddEvent: AddEvent;

  constructor(private addEventService: AddEventService, private router: Router) {
    super();
  }

  ngOnInit() {
    this.curaddEvent = {
      active: false,
      name: '',
      id: '',
      createTime: '',
      type: '',
      fileUrl: '',
      sort: '',
      status: '',
      fileId: '',
      eventId: '',
      eventUrlId: '',
      text: '',
      textId: '',
      fileIds: ''
    };
    this.searchEvent();
  }

  // 新增用户
  onAddUser() {
    this.isAddEditor = true;
    this.editorTitle = '新增事件';

    this.curaddEvent = {
      active: false,
      name: '',
      id: '',
      createTime: '',
      type: '',
      fileUrl: '',
      sort: '',
      status: '0',
      fileId: '',
      eventId: '',
      eventUrlId: '',
      text: '',
      textId: '',
      fileIds: ''
    };

    $('#data_editor').modal('show');
  }

// 修改
  onEditEvent(value: AddEvent) {
    this.curaddEvent = value;
    this.addEventService.getEventId(this.curaddEvent);
    this.router.navigate(['/buz/getEvent']);
  }

// 删除
  onDeleteEvent(value: AddEvent) {
    this.curaddEvent = value;
    $('#delete_confirm').modal('show'); // 显示编辑对话框
  }

// 新增/修改
  save() {
    if (this.isAddEditor) {// 新增数据
      this.curPage = 1;
      this.addEventService.addEventinfo(this.curaddEvent, this.fileId)
        .then(result => {
          if (result) {
            this.alert.info('新增成功');
            this.queryList();
          } else {
            this.alert.error('新增失败');
          }
        });
    } else { // 修改数据

    }

    $('#data_editor').modal('hide');
  }

  // 图标
  selectedFileOnChanged(event: any) {
    console.log(event.target.value);
    // 这里是文件选择完成后的操作处理
    this.uploader.queue[0].onSuccess = (response, status, headers) => {
      // 上传文件成功
      if (status === 200) {
        // 上传文件后获取服务器返回的数据
        const tempRes = JSON.parse(response);
        this.fileId = tempRes.systemFileTO.fileId;
      } else {
        // 上传文件后获取服务器返回的数据错误
      }
    };
    this.uploader.queue[0].upload(); // 开始上传
  }

  // 删除
  delete() {
    this.addEventService.removeEventinfo(this.curaddEvent)
      .then(result => {
        if (result) {
          this.alert.info('删除成功');
          this.queryList();
        } else {
          alert('数据删除失败，请重试！');
        }
        $('#delete_confirm').modal('hide');
      });
  }

  // 查询事件
  searchEvent() {
    this.curPage = 1;
    this.queryList();
  }

  onPage(event) {
    this.curPage = event;
    this.queryList();
  }

  private queryList() {
    this.addEventService.getEvent(this.curPage)
      .then(page => {
          this.addUserPage = page;
          this.addEvent = page.addEvent.datas;
        }
      );
  }
}
